package com.gitee.wsl.compose.ui.textfield.verific

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.gitee.wsl.compose.ui.base.TextField
import kotlinx.coroutines.delay

//https://juejin.cn/post/7249585135799697468?share_token=8f80733b-f388-4b0c-9427-abe49a861c6e

private enum class CodeState {
    ENTERED,//表示验证码已经完全输入
    INPUTTING,//表示验证码正在输入中,还未完成。
    PENDING,//表示验证码尚未开始输入。
}


@ExperimentalComposeUiApi
@Composable
fun TextField.VerificationCode(
    modifier: Modifier = Modifier,
    onVerify: (String) -> Unit = {}
) {
    val baseSize = 276
    BaseVerificationCodeTextField(
        onVerify = onVerify
    ) { codeLength, indxe, code ->
        // 判断当前位置是否有字符
        val isHasCode = indxe < code.length
        val fontSize = (144 / codeLength).sp

        val codeState = when {
            isHasCode -> CodeState.ENTERED
            (indxe == code.length) -> CodeState.INPUTTING
            else -> CodeState.PENDING
        }
        val cardColor = when (codeState) {
            CodeState.ENTERED -> Color(0xFF466Eff)
            CodeState.INPUTTING -> Color.White
            CodeState.PENDING -> Color(0xFFF5F5F5)
        }
        val elevation = when (codeState) {
            CodeState.ENTERED -> 3.dp
            CodeState.INPUTTING -> 6.dp
            CodeState.PENDING -> 0.dp
        }
        val textColor = when (codeState) {
            CodeState.ENTERED -> Color.White
            CodeState.INPUTTING -> Color.Gray
            CodeState.PENDING -> Color.Gray
        }

        val blinkInterval = 1000L
        var isVisible by remember { mutableStateOf(true) }
        LaunchedEffect(blinkInterval) {
            while (true) {
                isVisible = !isVisible
                delay(blinkInterval)
            }
        }
        key(elevation) {
            Card(
                Modifier.size((baseSize / codeLength).dp),
                colors = CardDefaults.cardColors(
                    containerColor = cardColor
                ),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = elevation,
                ),
            ) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    when (codeState) {
                        CodeState.ENTERED -> {
                            Text(
                                code[indxe].toString(), style = TextStyle(
                                    fontSize = fontSize,
                                    color = textColor,
                                    textAlign = TextAlign.Center
                                )
                            )
                        }

                        CodeState.INPUTTING -> {
                            if (isVisible) {
                                Text(
                                    "_", style = TextStyle(
                                        fontSize = fontSize,
                                        color = textColor,
                                        textAlign = TextAlign.Center
                                    )
                                )
                            }
                        }

                        CodeState.PENDING -> {
                            // Text(
                            //            "_", style = TextStyle(
                            //                fontSize = fontSize,
                            //                color = textColor,
                            //                 textAlign = TextAlign.Center
                            //            )
                            //        )
                        }
                    }
                }
            }
        }
    }
}